﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DomainModel.Abstract;
using System.Data.Linq;
using System.Configuration;
using System.Diagnostics;

namespace DomainModel.Concrete
{
    public class UserRepository:IUserRepository
    {
        private Table<User> usersTable;
        private DataContext dataContext;

        public object Locker { get; set; }

        public UserRepository()
        {
            
            String cs = ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString();
            dataContext = new DataContext(cs);
            usersTable = dataContext.GetTable<User>();
            Locker = new object();
        }

        public IQueryable<User> Users
        {
            get { return usersTable; }
        }

        public User GetUser(Int32 id)
        {
            try
            {
                return usersTable.First(d => d.UserID == id);
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public User GetUser(string login)
        {
            try
            {
                return usersTable.First(d => d.Login == login);
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public int GetClientsCountForManager(int id)
        {
            return usersTable.Where(d => (d.rf_UserID == id)).Count();        
        }

        public IQueryable<User> GetClients(bool all, int id)
        {
            if(all)
                return usersTable.Where(d => (d.rf_RoleID ==3));
            else
                return usersTable.Where(d => (d.rf_RoleID == 3 && d.rf_UserID==id));
        }

        public IQueryable<User> GetManagers()
        {
            return usersTable.Where(d => (d.rf_RoleID <=2));           
        }

        public int GetManagerId(string ManagerMail)
        {
            try
            {
                User usr = usersTable.First(d => (d.Email == ManagerMail)&&(d.rf_RoleID<3));
                if (usr != null)
                {
                    return usr.UserID;
                }
                return -1;
            }
            catch (Exception exc)
            {
                return -1;
            }
        }
        
        public bool CheckUser(string login)
        {
            try
            {
                return usersTable.Where(d => (d.Login == login)).Count() > 0;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public User GetUser(String login, String pass)
        {
            try
            {
                return usersTable.First(d => (d.Login == login && d.Password == pass));
            }
            catch (Exception exc)
            {
                return null;
            }
        }

        public void Add(User u)
        {
            try
            {
                usersTable.InsertOnSubmit(u);
            }
            catch (Exception exc)
            {

            }
        }

        public void Delete(User u)
        {
            usersTable.DeleteOnSubmit(u);
        }

        public void Save()
        {
            lock (this.Locker)
            {
                usersTable.Context.SubmitChanges();
                usersTable.Context.Refresh(RefreshMode.OverwriteCurrentValues, usersTable);
                Repositories.VUserRepository.RefreshContext();
            }
        }
    }
}
